﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using LumiSoft.Net.POP3.Client;
using LumiSoft.Net.Mime;
using Entity;
using System.Security;

namespace Utility
{
    public class MailTools
    {

        private static string ParseHeaderField(string fieldName, string headers)//获取邮件头属性值
        {
            //StringReader _rr = new StringReader(headers);
            //string ssss=_rr.ReadLine();
            //using (TextReader r = new StreamReader(new MemoryStream(System.Text.Encoding.ASCII.GetBytes(headers))))
            using (StringReader r = new StringReader(headers))
            {
                string line = r.ReadLine();

                while (line != null)
                {
                    // Find line where field begins
                    if (line.ToUpper().StartsWith(fieldName.ToUpper()))
                    {
                        // Remove field name and start reading value
                        string fieldValue = line.Substring(fieldName.Length).Trim();

                        // see if multi line value. See commnt above.
                        line = r.ReadLine();
                        while (line != null && (line.StartsWith("\t") || line.StartsWith(" ")))
                        {
                            fieldValue += line;
                            line = r.ReadLine();
                        }

                        return fieldValue;
                    }

                    line = r.ReadLine();
                }
            }

            return "";
        }

        private static string ParseHeaderFiledSubField(string fieldName, string subFieldName, string headers)//获取邮件头子属性值
        {
            string mainFiled = ParseHeaderField(fieldName, headers);
            // Parse sub field value
            if (mainFiled.Length > 0)
            {
                int index = mainFiled.ToUpper().IndexOf(subFieldName.ToUpper());
                if (index > -1)
                {
                    mainFiled = mainFiled.Substring(index + subFieldName.Length + 1); // Remove "subFieldName="

                    // subFieldName value may be in "" and without
                    if (mainFiled.StartsWith("\""))
                    {
                        return mainFiled.Substring(1, mainFiled.IndexOf("\"", 1) - 1);
                    }
                    // value without ""
                    else
                    {
                        int endIndex = mainFiled.Length;
                        if (mainFiled.IndexOf(" ") > -1)
                        {
                            endIndex = mainFiled.IndexOf(" ");
                        }

                        return mainFiled.Substring(0, endIndex);
                    }
                }
            }

            return "";
        }

        private static List<string> MailContentSplit(List<string> _mailBody, string _boundary)//拆分邮件主体
        {
            List<string> _buff = new List<string>();
            string _temp = null;
            string[] s = null;
            bool _needSplit = false;

            if (_boundary != null)
            {
                foreach (string item in _mailBody)
                {
                    _temp = item;
                    //_buff.Remove(_temp);
                    s = Regex.Split(_temp, _boundary);
                    //_buff.AddRange(s);
                    _mailBody.Remove(_temp);
                    for (int i = 1; i <= s.Length - 1; i++)
                    {
                        if (s[i] != "\r\n\r\n" && s[i] != "\r\n" && s[i] != "\n")
                            _mailBody.Add(s[i]);
                    }
                    break;
                }
                _mailBody = MailContentSplit(_mailBody, null);
            }
            else
            {
                foreach (string item in _mailBody)
                {
                    s = Regex.Split(item, "\r\n\r\n");
                    string s1 = null;
                    //_boundary = GetKeyValue("boundary", s[0]);
                    _boundary = MimeParser.ParseHeaderFiledSubField("content-type", "boundary", s[0]);

                    for (int i = 1; i < s.Length - 1; i++)
                    {
                        s1 += s[i] + "\r\n\r\n";
                    }
                    if (_boundary != "" && _boundary != null)
                    {
                        _temp = item;
                        //_buff.Remove(_temp);
                        s1 = s1.Replace(_boundary + "--", _boundary);
                        if (s1.IndexOf("--" + _boundary) >= 0)
                            s1 = s1.Replace("--" + _boundary, _boundary);
                        if (s1.IndexOf("-" + _boundary) >= 0)
                            s1 = s1.Replace("-" + _boundary, _boundary);
                        s = Regex.Split(s1, _boundary);
                        _needSplit = true;
                        break;
                    }
                    //_needSplit = false;
                    //string _temp = item;
                    //_mailBody.Remove(item);
                    //string[] s = Regex.Split(_temp, _boundary);
                    //_mailBody.AddRange(s);
                }
                if (_needSplit == true)
                {
                    _mailBody.Remove(_temp);
                    //_mailBody.AddRange(s);
                    for (int i = 1; i < s.Length - 1; i++)
                    {
                        _mailBody.Add(s[i]);
                    }

                    MailContentSplit(_mailBody, null);
                }
                //else
                //{
                //    return _mailBody;
                //}


            }

            return _mailBody;
            //return null;
        }

        private static string EncodeStringAnalysis(string _needAnalysis)//解析=??=类型字串
        {
            string[] _buff = null;
            if (_needAnalysis.IndexOf(" ") > -1 || _needAnalysis.IndexOf("\t") > -1)
            {
                string[] _temp = null;
                StringBuilder _sb = new StringBuilder();
                if (_needAnalysis.IndexOf(" ") > -1)
                    _temp = Regex.Split(_needAnalysis, " ");
                if (_needAnalysis.IndexOf("\t") > -1)
                    _temp = Regex.Split(_needAnalysis, "\t");

                for (int i = 0; i < _temp.Length; i++)
                {
                    _buff = _temp[i].Split('?');
                    _sb.Append(MailTools.MailDecoder(_buff[3], _buff[2], _buff[1]));
                }

                return _sb.ToString();
                //_mail.Subject = _sb.ToString();
            }
            else
            {
                _buff = _needAnalysis.Split('?');
                return MailTools.MailDecoder(_buff[3], _buff[2], _buff[1]);
                //_mail.Subject = MailTools.MailDecoder(_buff[3], _buff[2], _buff[1]);
            }

            return null;
        }

        private static string MailDecoder<T>(T _content, string _encoding, string _charset)//邮件解码
        {
            _encoding = _encoding.ToLower();
            _charset = _charset.ToLower();

            //if (_encoding.IndexOf(":") == 0)
            //    _encoding = _encoding.Replace(":", "").TrimStart(' ');
            //_encoding = _encoding.TrimStart(':').TrimStart(' ');

            if (_encoding == "b")
                _encoding = "base64";
            if (_encoding == "q")
                _encoding = "quoted-printable";
            if (_encoding == "")
                _encoding = "8bit";
            if (_charset == "gbk")
                _charset = "gb2312";

            string _returnCode = "";
            byte[] _tempBuff = null;

            switch (_encoding)
            {
                case "8bit":
                    //_tempBuff = Encoding.UTF8.GetBytes(_content);
                    if (_content is string)
                        return _content as string;

                    _tempBuff = _content as byte[];
                    break;
                case "7bit":
                    //_tempBuff = Encoding.UTF8.GetBytes(_content);
                    if (_content is string)
                        return _content as string;

                    _tempBuff = _content as byte[];
                    break;
                case "base64":
                    //_tempBuff = Convert.FromBase64String(_content);
                    if (_content is byte[])
                    {
                        //return Encoding.UTF8.GetString(_code as byte[]);
                        _tempBuff = _content as byte[];
                        break;
                    }
                    _tempBuff = Convert.FromBase64String(_content as string);
                    break;
                case "quoted-printable":
                    return _returnCode = QuotedPrintableDecoder(_content as string, _charset);
                    //break;
                default:
                    break;
            }
            
            switch (_charset)
            {
                case "gb2312":
                    Encoding gbk = Encoding.GetEncoding(936);
                    _returnCode = gbk.GetString(_tempBuff);
                    break;
                case "utf-8":
                    _returnCode = Encoding.UTF8.GetString(_tempBuff);
                    break;
                case "iso-8859-1":
                    Encoding isoType = Encoding.GetEncoding(28591);
                    _returnCode = isoType.GetString(_tempBuff);
                    break;
                case "gb18030":
                    Encoding gbk2 = Encoding.GetEncoding(54936);
                    _returnCode = gbk2.GetString(_tempBuff);
                    break;
                default:
                    break;
            }

            return _returnCode;
        }

        private static byte[] HexStringToByteArray(string hex)//十六进制转bytes
        {
            return Enumerable.Range(0, hex.Length)
                             .Where(x => x % 2 == 0)
                             .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                             .ToArray();
        }

        private static string QuotedPrintableDecoder(string _needDecode, string _charset)//Quoted-Printable解码
        {
            //_needDecode = "=D5=C5__=CC=CE=28ZH=2ET=29";
            //_needDecode = "=D5=C5_";
            //_charset = "gbk";
            _needDecode = _needDecode.Replace("=\r\n", "");
            StringBuilder _sb = new StringBuilder();
            Encoding _deCoder = null;
            switch (_charset)
            {
                case "utf-8":
                    _deCoder = Encoding.GetEncoding(65001);
                    break;
                case "gb2312":
                    _deCoder = Encoding.GetEncoding(936);
                    break;
                case "gb18030":
                    _deCoder = Encoding.GetEncoding(54936);
                    break;
                case "windows-1252":
                    _deCoder = Encoding.GetEncoding(1252);
                    break;
                default: break;
            }
            string _temp = "";
            string _returnCode = "";
            //int i=0;
            for (int i = 0; i < _needDecode.Length; i++)
            {
                if (_needDecode[i] == '=')
                {
                    _temp += _needDecode.Substring(i + 1, 2);
                    i = i + 2;
                }
                else
                {
                    if(_temp != "")
                        _sb.Append(_deCoder.GetString(HexStringToByteArray(_temp)));
                    _temp = "";

                    _sb.Append(_needDecode[i]);
                }
            }
                //string[] _buff = _needDecode.Split('=');
            if(_temp!="")
                _sb.Append(_deCoder.GetString(HexStringToByteArray(_temp)));
            
            return _sb.ToString();
        }

        public static void CreateFile(string _fileName, string _encoding, string _fileCodes)//创建下载文件
        {
            byte[] _fileBytes = null;
            string _filePath = @"C:\\Users\\Volker Zhang\\Desktop\\" + _fileName;
            FileStream fs = File.Create(_filePath);
            if (_encoding == "base64")
                _fileBytes = Convert.FromBase64String(_fileCodes);
                //_fileBytes = Convert.FromBase64String("suLK1NPDtcSzpM7EvP7D+3RleHTOxLz+uL28/g==");

            fs.Write(_fileBytes, 0, _fileBytes.Length);
            fs.Close();
        }

        public static Mail MailParser(string _mailUID, byte[] _mailBytes)//解析邮件主体
        {
            Mail _mail = new Mail();
            string _pattern = string.Empty;
            Match _mc;
            string _mailHeader;
            string _mailBody;
            string[] _buff;
            string _contentType = "";
            string _charset = "";
            string _encoding = "";
            string _boundary = "";
            string _tempContent = Encoding.UTF8.GetString(_mailBytes);
            //string _tempContent = Encoding.ASCII.GetString(_mailBytes);

            if (_tempContent.IndexOf("\r\n\n") >= 0)
                _tempContent = _tempContent.Replace("\r\n\n", "\r\n\r\n");
            if (_tempContent.IndexOf("\n\n") >= 0)
                _tempContent = _tempContent.Replace("\n\n", "\r\n\r\n");

            _buff = Regex.Split(_tempContent, "\r\n\r\n");
            _mailHeader = _buff[0];
            _mailBody = _tempContent.Replace(_mailHeader + "\r\n\r\n", "");
            if (_mailBody != "")
            {
                _contentType = ParseHeaderField("content-type", _mailHeader);
                if (_contentType.IndexOf("multipart") == -1)
                {
                    _charset = ParseHeaderFiledSubField("content-type", "charset", _mailHeader);
                    _encoding = ParseHeaderField("content-transfer-encoding", _mailHeader).TrimStart(':').TrimStart(' ');
                    if (_encoding.ToLower() != "quoted-printable")
                        _tempContent = MailDecoder(_mailBytes, _encoding, _charset);
                }
                else
                {
                    _buff = Regex.Split(_mailBody, "\r\n\r\n");
                    for (int j = 0; j < _buff.Length; j++)
                    {
                        if (_buff[j].ToLower().IndexOf("charset") > -1)
                            _charset = ParseHeaderFiledSubField("content-type", "charset", _buff[j]);
                        if (_buff[j].ToLower().IndexOf("content-transfer-encoding") > -1)
                            _encoding = ParseHeaderField("content-transfer-encoding", _buff[j]).TrimStart(':').TrimStart(' ');
                        if (_encoding.ToLower() != "quoted-printable" && _charset != "" && _encoding != "")
                        {
                            _tempContent = MailDecoder(_mailBytes, _encoding, _charset);
                            break;
                        }
                    }
                }

                if (_tempContent.IndexOf("\r\n\n") >= 0)
                    _tempContent = _tempContent.Replace("\r\n\n", "\r\n\r\n");
                if (_tempContent.IndexOf("\n\n") >= 0)
                    _tempContent = _tempContent.Replace("\n\n", "\r\n\r\n");

                _buff = Regex.Split(_tempContent, "\r\n\r\n");
                _mailHeader = _buff[0];
                _mailBody = _tempContent.Replace(_mailHeader + "\r\n\r\n", "");
            }

            ////////////////////////////////////////////////////////////////////////////////////////////////Mail UID
            _mail.MailUID = _mailUID;

            ////////////////////////////////////////////////////////////////////////////////////////////////Recived DateTime
            //string _date = MailTools.ParseHeaderField("date", _mailHeader).TrimStart(':').TrimStart(' ');
            string _date = ParseHeaderField("Received", _mailHeader);
            _buff = _date.Split(';');
            _mail.RecivedDateTime = GetDateTime(_buff[1].TrimStart('\t').TrimStart(' '));

            ////////////////////////////////////////////////////////////////////////////////////////////////From
            string _from = ParseHeaderField("from", _mailHeader).TrimStart(':').TrimStart(' ');
            _pattern = @".*<(.*)>";
            _mc = Regex.Match(_from, _pattern, RegexOptions.IgnoreCase);
            _mail.FromAddress = _mc.Groups[1].Value;
            if (_mail.FromAddress == "")
            {
                if (_from.IndexOf(" ") > -1)
                {
                    _buff = _from.Split(' ');
                    _mail.FromAddress = _buff[_buff.Length - 1];
                }
                else if (_from.IndexOf("\t") > -1)
                {
                    _buff = _from.Split('\t');
                    _mail.FromAddress = _buff[_buff.Length - 1];
                }
                else
                {
                    _mail.FromAddress = _from;
                }
            }
            _mail.FromName = _from.Replace("<" + _mail.FromAddress + ">", "").TrimStart(' ').TrimEnd(' ').Replace("\"", "");
            if (_mail.FromName == "")
                _mail.FromName = _mail.FromAddress;

            if (_mail.FromName.IndexOf("=?") > -1 && _mail.FromName.IndexOf("?=") > -1)
            {
                _buff = _mail.FromName.Split('?');
                _mail.FromName = MailDecoder(_buff[3], _buff[2], _buff[1]);
            }
            ////////////////////////////////////////////////////////////////////////////////////////////////To
            string _toList = ParseHeaderField("to", _mailHeader).TrimStart(':').TrimStart(' ');
            ContactNameCard _cnd = new ContactNameCard();
            if (_toList.IndexOf(",") > -1)
            {
                _buff = _toList.Split(',');
                for (int k = 0; k < _buff.Length; k++)
                {
                    if (_buff[k].IndexOf("<") > -1 && _buff[k].IndexOf(">") > -1)
                        _buff[k] = _buff[k].Replace("<", "").Replace(">", "");
                    _cnd.MailAddress = _buff[k];

                    _mail.To.Add(_cnd);
                }
            }
            else
            {
                if (_toList.IndexOf("<") > -1 && _toList.IndexOf(">") > -1)
                    _toList = _toList.Replace("<", "").Replace(">", "");
                _cnd.MailAddress = _toList;

                if (_toList != "")
                    _mail.To.Add(_cnd);
            }

            ////////////////////////////////////////////////////////////////////////////////////////////////Cc
            string _ccList = ParseHeaderField("cc", _mailHeader).TrimStart(':').TrimStart(' ');
            _cnd = new ContactNameCard();
            if (_ccList.IndexOf(",") > -1)
            {
                _buff = _ccList.Split(',');
                for (int k = 0; k < _buff.Length; k++)
                {
                    if (_buff[k].IndexOf("<") > -1 && _buff[k].IndexOf(">") > -1)
                        _buff[k] = _buff[k].Replace("<", "").Replace(">", "");
                    _cnd.MailAddress = _buff[k];

                    _mail.Cc.Add(_cnd);
                }
            }
            else
            {
                if (_ccList.IndexOf("<") > -1 && _ccList.IndexOf(">") > -1)
                    _ccList = _ccList.Replace("<", "").Replace(">", "");
                _cnd.MailAddress = _ccList;

                if (_ccList != "")
                    _mail.Cc.Add(_cnd);
            }

            ////////////////////////////////////////////////////////////////////////////////////////////////Bcc
            string _bccList = ParseHeaderField("bcc", _mailHeader).TrimStart(':').TrimStart(' ');
            _cnd = new ContactNameCard();
            if (_bccList.IndexOf(",") > -1)
            {
                _buff = _bccList.Split(',');
                for (int k = 0; k < _buff.Length; k++)
                {
                    if (_buff[k].IndexOf("<") > -1 && _buff[k].IndexOf(">") > -1)
                        _buff[k] = _buff[k].Replace("<", "").Replace(">", "");
                    _cnd.MailAddress = _buff[k];

                    _mail.Bcc.Add(_cnd);
                }
            }
            else
            {
                if (_bccList.IndexOf("<") > -1 && _bccList.IndexOf(">") > -1)
                    _bccList = _bccList.Replace("<", "").Replace(">", "");
                _cnd.MailAddress = _bccList;

                if (_bccList != "")
                    _mail.Bcc.Add(_cnd);
            }

            ////////////////////////////////////////////////////////////////////////////////////////////////Subject
            string _subject = ParseHeaderField("subject", _mailHeader).TrimStart(':').TrimStart(' ');
            _mail.Subject = _subject;
            if (_mail.Subject.IndexOf("=?") > -1 && _mail.Subject.IndexOf("?=") > -1)
                _mail.Subject = EncodeStringAnalysis(_mail.Subject);

            ////////////////////////////////////////////////////////////////////////////////////////////////Content & Attachment
            _contentType = ParseHeaderField("content-type", _mailHeader);
            if (_contentType.IndexOf("multipart") == -1)//纯text或html型
            {
                if (_contentType.ToLower().IndexOf("plain") > -1)
                {
                    //_charset = MailTools.ParseHeaderFiledSubField("content-type", "charset", _buff[0]);
                    //_encoding = MailTools.ParseHeaderField("content-transfer-encoding", _buff[0]).TrimStart(':').TrimStart(' ');

                    //_mailBody = _tempContent.Replace(_mailHeader + "\r\n\r\n", "");
                    _mail.TextContent = _mailBody.Replace(_buff[0] + "\r\n\r\n", "");
                    if (_encoding != "8bit" && _encoding != "7bit")
                        _mail.TextContent = MailDecoder(_mail.TextContent, _encoding, _charset);
                }
                else
                {
                    //_charset = MailTools.ParseHeaderFiledSubField("content-type", "charset", _buff[0]);
                    //_encoding = MailTools.ParseHeaderField("content-transfer-encoding", _buff[0]).TrimStart(':').TrimStart(' ');

                    _mail.HTMLContent = _mailBody.Replace(_buff[0] + "\r\n\r\n", "");
                    if (_encoding != "8bit" && _encoding != "7bit")
                        _mail.HTMLContent = MailDecoder(_mail.HTMLContent, _encoding, _charset);
                    //_mail.HTMLContent = MailTools.MailDecoder(_buff[1], _encoding, _charset);
                }
            }
            else//multipart混合型
            {
                _boundary = ParseHeaderFiledSubField("content-type", "boundary", _mailHeader);
                List<string> _mailBodyList = new List<string>();

                _mailBody = _mailBody.Replace(_boundary + "--", _boundary);
                if (_mailBody.IndexOf("--" + _boundary) >= 0)
                    _mailBody = _mailBody.Replace("--" + _boundary, _boundary);
                if (_mailBody.IndexOf("-" + _boundary) >= 0)
                    _mailBody = _mailBody.Replace("-" + _boundary, _boundary);

                _mailBodyList.Add(_mailBody);
                _mailBodyList = MailContentSplit(_mailBodyList, _boundary);

                Mail _tempMail = MailContentProcess(_mailBodyList);
                _mail.TextContent = _tempMail.TextContent;
                _mail.HTMLContent = _tempMail.HTMLContent;
                _mail.AttachmentFileList = _tempMail.AttachmentFileList;
                _mail.InlineFileList = _tempMail.InlineFileList;
            }
            //string _mailBody = _tempContent.Replace(_mailHeader + "\r\n\r\n", "");

            return _mail;
        }

        private static Mail MailContentProcess(List<string> _contentList)//处理邮件正文
        {
            Mail _mail = new Mail();
            AttachmentFiles _af;
            //_mail.AttachmentFileList = new List<AttachmentFiles>();
            //List<AttachmentFiles> _afList = new List<AttachmentFiles>();
            InlineFiles _if;
            //_mail.InlineFileList = new List<InlineFiles>();
            //List<InlineFiles> _ifList = new List<InlineFiles>();
            string _encoding = string.Empty;
            string _charset = string.Empty;
            string _name = string.Empty;

            foreach (string _item in _contentList)
            {
                string[] _buff = Regex.Split(_item, "\r\n\r\n");
                //if (MailTools.ParseHeaderField("content-type", _buff[0]).ToLower().IndexOf("text") > -1)
                if (ParseHeaderField("content-disposition", _buff[0]).ToLower() == "" && ParseHeaderField("content-id", _buff[0]) == "")
                {
                    if (ParseHeaderField("content-type", _buff[0]).ToLower().IndexOf("plain") > -1)
                    {
                        _charset = ParseHeaderFiledSubField("content-type", "charset", _buff[0]);
                        _encoding = ParseHeaderField("content-transfer-encoding", _buff[0]).TrimStart(':').TrimStart(' ');

                        //_mailBody = _tempContent.Replace(_mailHeader + "\r\n\r\n", "");
                        _mail.TextContent = _item.Replace(_buff[0] + "\r\n\r\n", "");
                        if (_encoding != "8bit" && _encoding != "7bit")
                            _mail.TextContent = MailDecoder(_mail.TextContent, _encoding, _charset);
                    }
                    else
                    {
                        _charset = ParseHeaderFiledSubField("content-type", "charset", _buff[0]);
                        _encoding = ParseHeaderField("content-transfer-encoding", _buff[0]).TrimStart(':').TrimStart(' ');

                        _mail.HTMLContent = _item.Replace(_buff[0] + "\r\n\r\n", "");
                        if (_encoding != "8bit" && _encoding != "7bit")
                            _mail.HTMLContent = MailDecoder(_mail.HTMLContent, _encoding, _charset);
                        //_mail.HTMLContent = MailTools.MailDecoder(_buff[1], _encoding, _charset);
                    }
                }
                else
                {
                    if (ParseHeaderField("content-disposition", _buff[0]).ToLower().IndexOf("attachment") > -1)
                    {
                        _af = new AttachmentFiles();
                        _af.FileName = ParseHeaderFiledSubField("content-disposition", "filename", _buff[0]);
                        if (_af.FileName.IndexOf("=?") > -1 && _af.FileName.IndexOf("?=") > -1)
                            _af.FileName = EncodeStringAnalysis(_af.FileName);

                        _af.Name = ParseHeaderFiledSubField("content-type", "name", _buff[0]);
                        if (_af.Name.IndexOf("=?") > -1 && _af.Name.IndexOf("?=") > -1)
                            _af.Name = EncodeStringAnalysis(_af.Name);
                        if (_af.Name == "")
                            _af.Name = _af.FileName;

                        _af.TransferEncoding = ParseHeaderField("content-transfer-encoding", _buff[0]).TrimStart(':').TrimStart(' ');

                        _af.FileCodes = _item.Replace(_buff[0] + "\r\n\r\n", "");
                        //_name = MailTools.ParseHeaderFiledSubField("content-type", "name", _buff[0]);
                        //MailTools.CreateFile(_af.FileName, _af.TransferEncoding, _af.FileCodes);
                        _mail.AttachmentFileList.Add(_af);
                    }
                    else
                    {
                        _if = new InlineFiles();
                        _if.FileName = ParseHeaderFiledSubField("content-disposition", "filename", _buff[0]);
                        if (_if.FileName.IndexOf("=?") > -1 && _if.FileName.IndexOf("?=") > -1)
                            _if.FileName = EncodeStringAnalysis(_if.FileName);
                        if (_if.FileName == "")
                        {
                            string[] s = ParseHeaderField("content-type", _buff[0]).Split('/');
                            _if.FileName = "未命名." + s[1];
                        }

                        _if.ContentID = ParseHeaderField("content-id", _buff[0]).TrimStart(':').TrimStart(' ').TrimStart('<').TrimEnd('>');

                        _if.TransferEncoding = ParseHeaderField("content-transfer-encoding", _buff[0]).TrimStart(':').TrimStart(' ');

                        _if.FileCodes = _item.Replace(_buff[0] + "\r\n\r\n", "");
                        //MailTools.CreateFile(_if.FileName, _if.TransferEncoding, _if.FileCodes);
                        _mail.InlineFileList.Add(_if);
                    }
                }
                //string _contentType = MailTools.ParseHeaderField("content-type", _buff[0]);
            }
            return _mail;
        }

        private static DateTime GetDateTime(string _recivedDateTime)//获取收件时间
        {
            string[] _englishMonth = new string[12] { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
            string[] _buff = _recivedDateTime.Split(' ');
            string _numMonth = string.Empty;
            for (int i = 0; i < _englishMonth.Length; i++)
            {
                if (_buff[2].ToLower() == _englishMonth[i])
                {
                    i++;
                    _numMonth = i.ToString();
                    break;
                }
            }
            //string sss = DateTime.Now.ToString();
            DateTime _returnCode = DateTime.Parse(_buff[3] + "/" + _numMonth + "/" + _buff[1] + " " + _buff[4]);

            return _returnCode;
        }
    }
}
